char[] and char* compatibility?

Posted by Aerovistae on Stack Overflow See other posts from Stack Overflow or by Aerovistae
Published on 2012-12-02T22:54:49Z Indexed on 2012/12/02 23:03 UTC
Read the original article Hit count: 203

Filed under:
|
|

In essence, will this code work? And before you say "Run it and see!", I just realized my cygwin didn't come with gcc and it's currently 40 minutes away from completing reinstallation. That being said:

char* words[1000];
for(int i = 0; i<1000; i++)
    words[i] = NULL;

char buffer[ 1024 ];

//omit code that places "ADD splash\0" into the buffer

if(strncmp (buffer, "ADD ", 4){
            char* temp = buffer + 4;
            printf("Adding: %s", temp);
            int i = 0;
            while(words[i] != NULL) i++;
            words[i] = temp;
}

I'm mostly uncertain about the line char* temp = buffer + 4, and also whether I can assign words[i] in the manner that I am. Am I going to get type errors when I eventually try to compile this in 40 minutes?

Also-- if this works, why don't I need to use malloc() on each element of words[]? Why can I say words[i] = temp, instead of needing to allocate memory for words[i] the length of temp?

© Stack Overflow or respective owner

Related posts about c

    Related posts about char-array